import { message } from "antd";
import * as bizUserService from '../services/user';

export default {
  namespace: 'bizUser',

  state: {
    pageData: {
      total: 0,
      size: 10,
      current: 1,
      pages: 1,
      records: []
    },
  },
  effects: {
    *page({ payload }, { call, put }) {
      console.log("page bizUsers");
      console.log(payload);
      const res = yield call(bizUserService.page, payload);
      if (res.code === 200) {
        yield put({
          type: 'savePageData',
          payload: res.data,
        });
      }
    },
    *get({ payload }, { call, put }) {
      console.log("get bizUser");
      const res = yield call(bizUserService.get, payload);
      if (res.code === 200) {
        yield put({
          type: 'test',
          payload: res,
        });
      }
    },
    *add({ payload }, { call, put, select }) {
      console.log("add bizUser");
      console.log(payload);
      const res = yield call(bizUserService.add, payload);
      const pageData = yield select(state => state.bizUser.pageData)
      if (res.code === 201) {
        message.success('添加成功');
        yield put({
          type: 'page',
          payload: {
            pageIndex: pageData.current,
            pageSize: pageData.size
          },
        });
      }
    },
    *update({ payload }, { call, put, select }) {
      console.log("update bizUser");
      console.log(payload);
      const res = yield call(bizUserService.update, payload);
      const pageData = yield select(state => state.bizUser.pageData)
      if (res.code === 201) {
        message.success('编辑成功');
        yield put({
          type: 'page',
          payload: {
            pageIndex: pageData.current,
            pageSize: pageData.size
          },
        });
      }
    },
    *del({ payload }, { call, put, select }) {
      console.log("delete bizUser");
      console.log(payload);
      const res = yield call(bizUserService.del, payload);
      const pageData = yield select(state => state.bizUser.pageData)
      if (res.code === 204) {
        message.success('删除成功');
        if (pageData.total % pageData.size === 1 && pageData.current > 1) {
          yield put({
            type: 'page',
            payload: {
              pageIndex: pageData.current - 1,
              pageSize: pageData.size
            },
          });
          return;
        }
        yield put({
          type: 'page',
          payload: {
            pageIndex: pageData.current,
            pageSize: pageData.size
          },
        });
      }
    },
    *delBatch({ payload }, { call, put, select }) {
      console.log("deleteBatch bizUser");
      console.log(payload);
      const res = yield call(bizUserService.delBatch, payload);
      const pageData = yield select(state => state.bizUser.pageData)
      if (res.code === 204) {
        message.success('删除成功');
        if (pageData.total % pageData.size === payload.length && pageData.current > 1) {
          yield put({
            type: 'page',
            payload: {
              pageIndex: pageData.current - 1,
              pageSize: pageData.size
            },
          });
          return;
        }
        yield put({
          type: 'page',
          payload: {
            pageIndex: pageData.current,
            pageSize: pageData.size
          },
        });
      }
    },
  },
  reducers: {
    savePageData(state, action) {
      return {
        ...state,
        pageData: action.payload,
      };
    },
  },
};